package com.example.android1.osstruts;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.android1.R;
import com.example.android1.osstruts.logs.retrofit.GitHubService;
//import com.github.moduth.blockcanary.BlockCanary;
//import com.squareup.leakcanary.LeakCanary;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

//import okhttp3.Call;
//import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

//常用开源框架的使用。
public class OsStructsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_os_structs);

        netRequestWithRetrofit();

    }

    String url = "http://图片地址";
    ImageView imageView;

    public void loadImageSimple(View view) {
        Glide.with(getApplicationContext()).load(url).into(imageView);
    }

    public void loadImage(View view) { //常用参数配置
        Glide.with(getApplicationContext()) // 指定Context
                .load(url)// 指定图片的URL
                .placeholder(R.mipmap.ic_launcher)// 指定图片未成功加载前显示的图片
                .error(R.mipmap.ic_launcher)// 指定图片加载失败显示的图片
                .override(300, 300)//指定图片的尺寸
                .fitCenter()//指定图片缩放类型为
                .centerCrop()// 指定图片缩放类型为
                .skipMemoryCache(true)// 跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE)//跳过磁盘缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)//仅仅只缓存原来的全分辨率的图像
                .diskCacheStrategy(DiskCacheStrategy.RESULT)//仅仅缓存最终的图像
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存所有版本的图像
                .priority(Priority.HIGH)//指定优先级.Glide将会用他们作为一个准则，并尽可能的处理这些请求，但是它不能保证所有的图片都会按照所要求的顺序加载。优先级排序:IMMEDIATE > HIGH > NORMAL >　LOW
                .into(imageView);//指定显示图片的Imageview
    }


    public void netRequestWithRetrofit() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //支持RxJava
                .addConverterFactory(GsonConverterFactory.create()) //将返回的httpResponse 转换为Java对象
                .build();
        GitHubService service = retrofit.create(GitHubService.class);
        Call repos = (Call) service.listRepos("tom"); //构建okHttp的Call对象。
        repos.enqueue(new Callback() { //开始异步请求
            @Override
            public void onResponse(Call call, retrofit2.Response response) {
                System.out.println("请求成功：" + response.body());
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                System.out.println("请求失败，onFailure");
            }
        });
    }

    OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.SECONDS).build();

    public void asyRequest() {
        Request request = new Request.Builder().url("http://www.baidu.com").get().build();
        okhttp3.Call call = client.newCall(request);//2.将Request对象封装成Call对象
//        Response response = call.execute();//3.同步请求
        //System.out.println(response.body().string());
        call.enqueue(new okhttp3.Callback() {//3.异步请求
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                System.out.println("Fail"); //子线程中
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                //4.处理返回数据，目前还是子线程中
                System.out.println(response.body().string());
            }
        });
    }


}